//
//  HHRecommendTagsController.m
//  百思不得姐
//
//  Created by hjl on 16/4/13.
//  Copyright © 2016年 hjl. All rights reserved.
//

#import "HHRecommendTagsController.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import "HHRecommendTagCell.h"
#import <SVProgressHUD.h>
#import "HHRecommendTag.h"

@interface HHRecommendTagsController ()

/** tags数组 */
@property (nonatomic, strong) NSArray *recommendTags;


@end

@implementation HHRecommendTagsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"推荐标签";
    self.view.backgroundColor = HHBackgroundColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"HHRecommendTagCell" bundle:nil] forCellReuseIdentifier:@"recommendTagCell"];
    
    self.tableView.rowHeight = 80;
    // 取消分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self getRecommendTags];
    
}

- (void)getRecommendTags{
    
    NSString *strUrl  = @"http://api.budejie.com/api/api_open.php";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"tag_recommend";
    parameters[@"action"] = @"sub";
    parameters[@"c"] = @"topic";
    
    [[AFHTTPSessionManager manager] GET:strUrl parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //HHLog(@"%@", responseObject);
        
        // 字典数组转模型数组
        self.recommendTags = [HHRecommendTag mj_objectArrayWithKeyValuesArray:responseObject];
        
        // 刷新数据
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 指示器提示加载错误
        [SVProgressHUD showErrorWithStatus:@"网络加载失败"];
    }];

}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.recommendTags.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HHRecommendTagCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recommendTagCell" forIndexPath:indexPath];
    
    cell.recommendTag = self.recommendTags[indexPath.row];
    
    return cell;
}


@end
